package com.chushouya.order.dao.repository;

import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.OrderProductEntity;
import com.chushouya.order.dao.mapper.OrderProductEntityMapper;
import com.chushouya.order.dto.admin.orderproduct.OrderProductQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;


@Repository
public class OrderProductRepository extends BaseRepository<OrderProductEntityMapper, OrderProductEntity, Long> {

    public List<OrderProductEntity> selectOrderGoodsListResult(Long orderId) {
        Example example = this.buildExample();
        example.and().andEqualTo("orderId", orderId);
        return this.selectByExample(example);
    }

    public OrderProductEntity getOrderProduct(Long orderId) {
        Example example = this.buildExample();
        example.and().andEqualTo("orderId", orderId);
        return this.selectFirstOneByExample(example);
    }


    public List<OrderProductEntity> selectListByOrderIds(List<Long> orderIds) {
        if (Lists.isEmpty(orderIds)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andIn("orderId", orderIds);
        return selectByExample(example);
    }


    public boolean existsBarCode(String productBarCode) {
        final Example example = this.buildExample();
        example.and().andEqualTo("productBarCode", productBarCode);
        return this.selectCountByExample(example) > 0;
    }

    public void setProductBarCode(Long orderId, String productBarCode) {
        Example where = super.buildExample();
        where.and().andEqualTo("orderId", orderId);
        OrderProductEntity update = new OrderProductEntity();
        update.setUpdateTime(Dates.getTimeNow());
        this.updateBySelective(update, where);
    }

    public OrderProductEntity selectOneByProductBarCode(String productBarCode) {
        final Example example = this.buildExample();
        example.and().andEqualTo("productBarCode", productBarCode);
        example.orderBy("orderProductId").desc();
        return this.selectFirstOneByExample(example);
    }

    public OrderProductEntity selectOne(OrderProductQuery query){
        return this.selectFirstOneByExample(buildExample(query));
    }

    private Example buildExample(OrderProductQuery query){
        Example example = this.buildExample();
        Example.Criteria criteria = example.createCriteria();
        if(Objects.nonNull(query.getOrderProductId())){
            criteria.andEqualTo("orderProductId", query.getOrderProductId());
        }
        if(Objects.nonNull(query.getOrderId())){
            criteria.andEqualTo("orderId", query.getOrderId());
        }
        if(Objects.nonNull(query.getProductId())){
            criteria.andEqualTo("productId", query.getProductId());
        }
        return example;
    }

    public List<OrderProductEntity> getProductListByOrderId(Long orderId) {
        OrderProductQuery query = new OrderProductQuery();
        query.setOrderId(orderId);
        return this.selectByExample(buildExample(query));
    }

    public OrderProductEntity selectByOrderId(Long orderId) {
        OrderProductQuery query = new OrderProductQuery();
        query.setOrderId(orderId);
        return this.selectOne(query);
    }
}
